#!/usr/bin/perl

use lib::abs qw( base_lib lib );
use JIP::ToolSet; # load my commonly-used modules in a single import

use Getopt::Long qw( GetOptions );       # CPAN modules
use Pod::Usage qw( pod2usage );          #
use English qw( -no_match_vars );        #
use File::Spec::Functions qw( catfile ); #
use FindBin qw( $RealBin );              #

use Log::Console; # base lib

use Net::FTP::Client;      # searcher custom modules
use OutputWriter::Crawler; #

#-- ui -------------------------------------------------------------------------
my $FTPUser    = 'anonymous';        # default settings
my $FTPPass    = 'upyachka_fighter'; #
my $FTPTimeout = 15;                 #

GetOptions(
    'help'          => \my $isHelp,
    'ip=s'          => \my $FTPHost,
    'output_file=s' => \my $outputFile,

    'user=s'    => \$FTPUser,
    'pass=s'    => \$FTPPass,
    'timeout=i' => \$FTPTimeout,
);

pod2usage( -exitstatus => 0, -output => \*STDOUT, -verbose => 2 ) if $isHelp;

pod2usage(
    -exitstatus => 1,
    -output     => \*STDERR,
    -message    => 'required parameter "ip" not defined'
) unless $FTPHost;

pod2usage(
    -exitstatus => 1,
    -output     => \*STDERR,
    -message    => 'required parameter "output_file" not defined'
) unless $outputFile;

#-- init log -------------------------------------------------------------------
our $log = Log::Console->new( log_level => 'all' );

#-- signal handlers ------------------------------------------------------------
require( catfile( $RealBin, 'signal_handlers.pl' ) );

#-- FTP-client -----------------------------------------------------------------
$log->Note('init FTP-connection');

my $FTPClient = Net::FTP::Client->new(
    host    => $FTPHost,
    user    => $FTPUser,
    pass    => $FTPPass,
    timeout => $FTPTimeout,
);

#-- output object --------------------------------------------------------------
my $outputObject = OutputWriter::Crawler->new( output_file => $outputFile );

#-- main processing ------------------------------------------------------------
$log->Note('-- seed generator -----------------------------------------------');

my @seeds;

foreach ( $FTPClient->dir( '' ) ) {
    if ( $ARG->{'type'} eq 'd' ) {
        $log->Debug( "seed: ". $ARG->{'name'} );

        push @seeds, '/'. $ARG->{'name'};
    }
    elsif ( $ARG->{'type'} eq 'f' ) {
        $outputObject->saveFileMetadata(
            name => $ARG->{'name'},
            path => '/',
            size => $ARG->{'size'},
        );
    }
}

$log->Note('-- crawl loop ---------------------------------------------------');

foreach my $eachSeed ( @seeds ) {
    $log->Note( "scanning: '". $eachSeed ."'" ) and indexing( $eachSeed )
}

$log->Note('-- exiting ------------------------------------------------------');

#-- subroutines ----------------------------------------------------------------
sub indexing {
    my $currentDir = shift;

    $outputObject->saveDirMetadata( path => $currentDir );

    foreach ( $FTPClient->dir( $currentDir ) ) {
        if ( $ARG->{'type'} eq 'f' ) {
            $outputObject->saveFileMetadata(
                name => $ARG->{'name'},
                path => $currentDir,
                size => $ARG->{'size'},
            );
        }
        elsif ( $ARG->{'type'} eq 'd' ) {
            indexing( $currentDir .'/'. $ARG->{'name'} );
        }
    }
}

#-- end ------------------------------------------------------------------------

__END__

=head1 NAME

ftp_crawler.pl - simple FTP crawler

=head1 SYNOPSIS

ftp_crawler.pl [options]

Options:

--help            display this text

--ip              ip adress

--user            for remote server auth ('anonymous' by default)

--pass            for remote server auth ('upyachka_fighter' by default)

--timeout         set a timeout value (15 by default)

--output_file     output file

=cut
